#include "SpaceInvadersPCH.h"
#include "WeaponManager.h"

void WeaponManager::Initialise()
{
	RequestData(spaceinvaders::prehash::hs_Bullets);
	bullets.reserve(cMaxBullets);
}

void WeaponManager::Shutdown()
{
	ReleaseData();
}

void WeaponManager::Step()
{
	for (std::vector<BulletRecord>::iterator it = bullets.begin(), end = bullets.end(); it != end;)
	{
		BulletRecord& record = *it;
		record.position += record.velocity;
		if (record.position.getZ() > 200.0f)
		{
			it = bullets.erase(it);
			end = bullets.end();
		}
		else
		{
			++it;
		}
	}
}

void WeaponManager::Draw(DrawContext& drawContext)
{
	if (m_pRenderData)
	{
		for (std::vector<BulletRecord>::iterator it = bullets.begin(), end = bullets.end(); it != end; ++it)
		{
			BulletRecord& record = *it;
			gp::ParameterBlockScratch<1> params;
			cr::Matrix4f transform = cr::Matrix4f::Identity;
			transform.m_translation = record.position.getXYZ_W1();
			params.AddParameter()->Set(spaceinvaders::prehash::hs_g_objectToWorld, spaceinvaders::prehash::hs_OBJECTTOWORLD, gk::GpuProgramParameterType::Matrix, transform);
			drawContext.PushParameterBlock(params);
			drawContext.DrawModels(m_pRenderData);
			drawContext.PopParameterBlock();
		}
		
	}
}

void WeaponManager::AddBullets(cr::Vec4fRef startPos)
{
	if (bullets.size() < cMaxBullets)
	{
		bullets.push_back(BulletRecord());
		bullets.back().position = startPos;
		bullets.back().velocity = cr::Vec4f(0.0f,0.0f,3.0f,0.0f);
	}
}